package com.naihe.anderson.sms.activity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.util.FileUtil;
import com.naihe.anderson.sms.util.ImageUtil;

public class PicShowActivity extends Activity {

	NhSmsApp nhSmsApp;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String bitmapName = getIntent().getStringExtra("bitmapName");
        setContentView(R.layout.pic_show_layout);
        
        nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		
        OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				nhSmsApp.setActivity(null);
				PicShowActivity.this.finish();
				System.gc();
			}
		};
        
		ImageButton image_close = (ImageButton) findViewById(R.id.image_close);
		image_close.setOnClickListener(listener);
		
        ImageView imageView = (ImageView) findViewById(R.id.image);
        Bitmap backMap = getLoacalBitmap(bitmapName);
        if(backMap != null)
        	imageView.setImageBitmap(backMap);
        else
        	imageView.setImageDrawable(getResources().getDrawable(R.drawable.error_pic));
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 截获按键事件
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			nhSmsApp.setActivity(null);
			PicShowActivity.this.finish();
			System.gc();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
	
	private Bitmap getLoacalBitmap(String draw_name) {
		Bitmap backMap = null;
		try {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inSampleSize = 1;
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true; // 获取资源图片
			FileInputStream fis = new FileInputStream(FileUtil.getPicNotePath() + draw_name);
			Bitmap bitmap = BitmapFactory.decodeStream(fis, null, opt);
			if(bitmap != null)
				backMap = ImageUtil.zoomBitmap(bitmap, 1.5f, 1.5f);
		} catch (FileNotFoundException e) {
		}
		return backMap;
	}
}
